package org.lmind.arena.srd35.game;

import java.util.ArrayList;
import java.util.HashMap;

import org.lmind.arena.srd35.Creature;
import org.lmind.arena.srd35.rule.BasicRule;

public class CombatStage {
	
	private BasicRule rule;
	
	private HashMap<Creature, CombatProperty> creatures = new HashMap<Creature, CombatProperty>();
	
	private ArrayList<CombatProperty> activeQueue;
	
	private CombatProperty active;
	
	public CombatStage(BasicRule rule) {
		this.rule = rule;
	}
	
	public Creature[] getCreatures() {
		return null;
	}
	
	public Creature[] getPlayerTeam() {
		return null;
	}
	
	public Creature getActiveCreature() {
		return active.getCreature();
	}
	
	public void next() {
		
		int index = activeQueue.indexOf(active);
		if (index >= activeQueue.size()) {
			
			active = activeQueue.get(0);
			// TODO next round event
			
		} else {
			active = activeQueue.get(index + 1);
		}
	}
	
	public CombatProperty getCombatProperty(Creature creature) {
		return null;
	}
	
	public void initiative() {
		activeQueue = initiativeCheck();
	}
	
	public void addCreature(Creature creature, int x, int y) {
		CombatProperty prop = new CombatProperty();
		prop.setX(x);
		prop.setY(y);
		prop.setInitiativeCheck(rule.initiativeCheck(creature));
		prop.setCreature(creature);
		
		for (int i = activeQueue.size() - 1; i >= 0; i--) {
			if (activeQueue.get(i).getInitiativeCheck() >= prop.getInitiativeCheck()) {
				activeQueue.add(i + 1, prop);
				break;
			}
		}
		creatures.put(creature, prop);
	}
	
	public void removeCreature(Creature creature) {
		CombatProperty prop = creatures.remove(creature);
		if (prop != null)
			activeQueue.remove(prop);
	}
	
	
	/**
	 * 进行initiativeCheck
	 * @return
	 */
	private ArrayList<CombatProperty> initiativeCheck() {
		
		ArrayList<CombatProperty> list = new ArrayList<CombatProperty>();
		
		// 做一个标志，方便之后排序
		CombatProperty flag = new CombatProperty();
		flag.setInitiativeCheck(-1);
		list.add(flag);
		
		for (CombatProperty unit : activeQueue) {
			
			unit.setInitiativeCheck(rule.initiativeCheck(unit.getCreature()));
			
			for (int i = 0; i < list.size(); i++) {
				CombatProperty t = list.get(i);
				// initiativeCheck较高者在前
				if (unit.getInitiativeCheck() == t.getInitiativeCheck()) {
					// 如果initiativeCheck相同，则modifier最高者优先，如果仍然相同，则两者之间循环进行InitiativeCheck直到不同
					if (rule.initiativeModifier(unit.getCreature()) == rule.initiativeModifier(t.getCreature())) {
						if (initiativeCheckLoop(unit, t)) {
							list.add(i, unit);
						} else {
							list.add(i + 1, unit);
						}
						break;
					} else if (rule.initiativeModifier(unit.getCreature()) > rule.initiativeModifier(t.getCreature())) {
						list.add(i, unit);
						break;
					} else if (rule.initiativeModifier(unit.getCreature()) < rule.initiativeModifier(t.getCreature())) {
						list.add(i + 1, unit);
						break;
					}
				} else if (unit.getInitiativeCheck() < t.getInitiativeCheck()) {
					list.add(i, unit);
					break;
				}
			}
		}
		list.remove(list.size() - 1);
		return list;
	}
	
	/**
	 * 循环进行initiativeCheck直到分出胜负
	 * @param a
	 * @param b
	 * @return
	 */
	private boolean initiativeCheckLoop(CombatProperty a, CombatProperty b) {
		int t1 = 0, t2 = 0;
		while (t1 != t2) {
			t1 = rule.initiativeModifier(a.getCreature()) + rule.roll(20, 1, 0);
			t2 = rule.initiativeModifier(b.getCreature()) + rule.roll(20, 1, 0);
		}
		return t1 > t2;
	}
}
